package com.ganzalang.gmall.designpatterns.chain;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: 使用责任链模式
 * @author: ganzalang
 */
@Slf4j
public class WithChainPatternDemo {

    public static void main(String[] args) {
        ChainA chainA = new ChainA();
        ChainB chainB = new ChainB();
        ChainC chainC = new ChainC();

        chainC.setHandler(chainA);
        chainA.setHandler(chainB);
        chainC.execute();
    }

    public interface Handler {
        void execute();
        void doExecute();
    }

    public static abstract class AbstractHandler implements Handler {

        private Handler handler;

        public void setHandler(Handler handler) {
            this.handler = handler;
        }

        @Override
        public void execute() {
            doExecute();
            if (handler != null ) {
                handler.execute();
            }
        }
    }

    public static class ChainA extends AbstractHandler {

        @Override
        public void doExecute() {
            log.info("执行A方法");
        }
    }

    public static class ChainB extends AbstractHandler {

        @Override
        public void doExecute() {
            log.info("执行B方法");
        }
    }

    public static class ChainC extends AbstractHandler {

        @Override
        public void doExecute() {
            log.info("执行C方法");
        }
    }


}
